from pms.common.views import PostFormula
from info.models import Notice
from info.utils.notice import notice_view
from department.utils import assert_one_of_department_permissions, assert_department_permissions
from project.utils.permission import assert_one_of_project_permissions, assert_project_permissions
from user.utils.permission import assert_one_of_system_permissions, assert_system_permissions


class API(PostFormula):
    def handle(self, req):
        notice = Notice.objects.get(id=req['id'])
        view = notice_view(notice)
        if notice.did is not None:
            if self.user == notice.uid:
                assert_one_of_department_permissions(self.user.id, notice.did_id, [6, 7])
            else:
                assert_department_permissions(self.user.id, notice.did_id, [7])
        elif notice.pid is not None:
            if self.user == notice.uid:
                assert_one_of_project_permissions(notice.pid_id, self.user.id, [8, 9])
            else:
                assert_project_permissions(notice.pid_id, self.user.id, [9])
        else:
            if self.user == notice.uid:
                assert_one_of_system_permissions(self.user.id, [17, 18])
            else:
                assert_system_permissions(self.user.id, [18])
        notice.delete()
        return self.success_respond(view)
